######################################################################################
#
# This file was provided as part of the SharePoint Mystery Deployment package
# Created by Christopher Kimbell (http://sharepointmystery.com)
#
######################################################################################

# -----------------------------------------------------------------
# Creates an object used to manipulate the Web Analytics Service
# -----------------------------------------------------------------
function New-WebAnalyticsService()
{
    $obj=new-object PSObject -Property @{
      Name = "Web Analytics Service";
      Proxy = "Web Analytics Service Proxy";
      Pool = New-ApplicationPool;
      StagingDB = New-Database;
      ReportingDB = New-Database;
      Service = New-SupportingService;
      ProcessingService = New-SupportingService;
      ReportingDataRetentionPeriod = 25;
      MaxAggregationDepth = "WebApplication";
      MinAggregationDepth = "WebApplication"
      SamplingRate = 100;
      EnableDataTrimming = $false;
      ListOfReportingDatabases = "";
      ListOfStagingDatabases = "";
      ReportConsolidationStartTime = 3;
    }
    $obj.Service.Name = "Web Analytics Web Service"
    $obj.ProcessingService.Name = "Web Analytics Data Processing Service"
    # -----------------------------------------------------------------
    # Validates the information in the object
    # -----------------------------------------------------------------
    Add-Method $obj "Validate" `
    {
        Write-Operation "Validating Web Analytics Service"
        $this.Pool.Validate()
        $this.StagingDB.Validate()
        $this.ReportingDB.Validate()
        $this.Service.Validate()
        $this.ProcessingService.Validate()
        
        if($this.ListOfReportingDatabases -eq "")
        {
            $this.ListOfReportingDatabases = "<ReportingDatabases><ReportingDatabase ServerName='$($this.ReportingDB.Server)' DatabaseName='$($this.ReportingDB.FullName)'/></ReportingDatabases>"
        }
        if($this.ListOfStagingDatabases -eq "")
        {
            $this.ListOfStagingDatabases = "<StagingDatabases><StagingDatabase ServerName='$($this.StagingDB.Server)' DatabaseName='$($this.StagingDB.FullName)'/></StagingDatabases>"
        }
        
        Validate-NotEmpty "Name" $this.Name
        Validate-NotEmpty "Proxy" $this.Proxy
        Validate-Range "ReportingDataRetentionPeriod" $this.ReportingDataRetentionPeriod  0 25
        Validate-Range "SamplingRate" $this.SamplingRate  0 100
        Validate-Range "ReportConsolidationStartTime" $this.ReportConsolidationStartTime  1 23
        Validate-MemberOf "MaxAggregationDepth" $this.MaxAggregationDepth "All", "WebApplication","SiteCollection", "Site"
        Validate-MemberOf "MinAggregationDepth" $this.MinAggregationDepth "WebApplication","SiteCollection", "Site"
    }
    # -----------------------------------------------------------------
    # Provisions the Service
    # -----------------------------------------------------------------
    Add-Method $obj "Provision" `
    {
        $sa = Get-SPServiceApplication  | Where-Object {$_.DisplayName -eq $this.Name}

        if($sa -eq $null)
        {
            Write-Operation "Creating $($this.Name)"
            $pool = $this.Pool.Provision()
            
            $this.StagingDB.Provision()
            $this.StagingDB.AddRole($farm.DeploymentAccount, "db_owner")
            $this.StagingDB.AddRole($this.Pool.Account, "db_owner")
            $this.ReportingDB.Provision()
            $this.StagingDB.AddRole($farm.DeploymentAccount, "db_owner")
            $this.StagingDB.AddRole($this.Pool.Account, "db_owner")
            
            $sa = New-SPWebAnalyticsServiceApplication -Name $this.Name `
                    -ApplicationPool $pool `
                    -SamplingRate $this.SamplingRate `
                    -ListOfReportingDatabases $this.ListOfReportingDatabases `
                    -ListOfStagingDatabases $this.ListOfStagingDatabases `
                    -ReportingDataRetentionPeriod $this.ReportingDataRetentionPeriod `
                    -MaxAggregationDepth $this.MaxAggregationDepth `
                    -MinAggregationDepth $this.MinAggregationDepth `
                    -EnableDataTrimming:$this.EnableDataTrimming
        }
        else
        {
            Write-OperationDone "$($this.Name) has already been provisioned"  
        } 
         
        $proxy = Get-SPServiceApplicationProxy  | Where-Object {$_.DisplayName -eq $this.Proxy}  
        if($proxy -eq $null)
        {
            New-SPWebAnalyticsServiceApplicationProxy -Name $this.Proxy -ServiceApplication $sa
        }
        else
        {
            Write-OperationDone "$($this.Proxy) has already been provisioned"  
        }
        
        Set-SPWebAnalyticsServiceApplication -Identity $sa `
            -ReportConsolidationStartTime $this.ReportConsolidationStartTime
        
        $this.Service.Provision()
        $this.ProcessingService.Provision()
       
        return $sa
    }
    # -----------------------------------------------------------------
    # Unprovisions the Service
    # -----------------------------------------------------------------
    Add-Method $obj "Unprovision" `
    {
        Remove-ServiceApplicationProxy $this.Proxy
        Remove-ServiceApplication $this.Name
        $this.Service.Unprovision()
        $this.ProcessingService.Unprovision()
    }
    return $obj
}
